# 申购单
from api_setting import *
from dim_api_auto_test.DimAPI.core.request_client import RestClient
from dim_api_auto_test.DimAPI.search import Search
from tools.database import MySQLClient


class PurchaseApply(RestClient):
    def __init__(self, **kwargs):
        super(PurchaseApply, self).__init__(**kwargs)
        self.search = Search()
        self.mysql = MySQLClient.setup()

    def remove_purchase_apply(self):
        """
        删除草稿状态的申购单
        :return:
        """
        url = REMOVE_PURCHASE_APPLY_URL
        # 通过SQL查询草稿状态的申购单，并且获取第1条数据的id
        order_id = self.mysql.select(DRAFT_PC_APPLY_ID_SQL)[0]["id"]
        params = {"ids": order_id}
        r = self.get(url=url, params=params)
        return r.content

    def pc_apply_cancel_confirm(self):
        """
        申购单取消提交
        :return:
        """
        url = UPDATE_PC_APPLY_STATUS_URL
        # 查询状态为已提交待采购、物料种类为五金的申购单，并且获取第1个申购单号
        order_no = self.mysql.select(COMMITTED_AND_W_PC_APPLY_ID_SQL)[0]["order_no"]
        params = {"orderNo": order_no, "status": "Draft", "detailsStatus": "Draft"}
        r = self.post(url=url, params=params)
        return r.content


if __name__ == "__main__":
    test = PurchaseApply()
    res = test.pc_apply_cancel_confirm()
    print(res)
